import 'package:flutter/cupertino.dart';
import 'package:flutter/material.dart';
import 'package:flutter_easyrefresh/easy_refresh.dart';

class Market extends StatefulWidget {
  @override
  State<StatefulWidget> createState() {
    return _MarketState();
  }
}

class _MarketState extends State<Market> {
  EasyRefreshController _controller;
  int _count = 20;

  @override
  void initState() {
    super.initState();
    _controller = EasyRefreshController();
  }

  @override
  Widget build(BuildContext context) {
    return EasyRefresh.custom(
      enableControlFinishRefresh: false,
      enableControlFinishLoad: true,
      controller: _controller,
      header: ClassicalHeader(
        refreshedText: "刷新完成",
        refreshingText: "正在刷新",
        refreshFailedText: "刷新失败",
        refreshText: "下拉刷新",
        refreshReadyText: "准备刷新",
        showInfo: false
      ),
      footer: ClassicalFooter(),
      onRefresh: () async {
        await Future.delayed(Duration(seconds: 2), () {
          print('onRefresh');
          setState(() {
            _count = 20;
          });
          _controller.resetLoadState();
        });
      },
      onLoad: () async {
        await Future.delayed(Duration(seconds: 2), () {
          print('onLoad');
          setState(() {
            _count += 10;
          });
          _controller.finishLoad(noMore: _count >= 40);
        });
      },
      slivers: <Widget>[
        SliverList(
          delegate: SliverChildBuilderDelegate(
            (context, index) {
              return Container(
                width: 60.0,
                height: 60.0,
                child: Center(
                  child: Text('$index'),
                ),
                color: index % 2 == 0 ? Colors.grey[300] : Colors.transparent,
              );
            },
            childCount: _count,
          ),
        ),
      ],
    );
  }
}
